package algorithm.easy;

public class Solution27 {

    //输入：nums = [0,1,2,2,3,0,4,2], val = 2
    //输出：5, nums = [0,1,4,0,3]
    //解释：函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面
    //的元素。
    public static int removeElement(int[] nums, int val) {
        int res = 0;
        for(int i=0;i<nums.length;i++){
            if(nums[i] != val){
                nums[res++] = nums[i];
            }
        }
        int[] dest = new int[res];
        System.arraycopy(nums,0,dest,0,res);
        return res;
    }

    public static void main(String[] args){
        //输入：nums = [0,0,1,1,1,2,2,3,3,4]
        //输出：5, nums = [0,1,2,3,4]
        int[] nums = {0,1,2,2,3,0,4,2};
        int val = 2;
        System.out.println(removeElement(nums,val));
    }
}
